﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports DotNetNuke
Namespace NVPortal.Modules.NVHoiVien.Search

    Public MustInherit Class inc_SerachResult
        Inherits Entities.Modules.PortalModuleBase
        Private Datefrom As Date = "01/01/2000"
        Private Dateto As Date = "01/01/2020"
        Private Title As String = ""
        Private KeySearch As String = ""
        Private khuvucid As Integer = 0
        Private linhvuckdid As Integer = 0
        Private quymoid As Integer = 0
        Public CategoryName As String = "Kết quả tìm kiếm Hội viên"
        Private PageIndex As Integer = 1
        Private ControlSearch As Integer = 1
        Private Type As Integer = 1

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not IsPostBack Then
                    'Get info from Url

                    If IsNumeric(Request.Item("timkiem")) Then
                        ControlSearch = Request.Item("timkiem")
                        Me.rbtChoice.SelectedValue = ControlSearch
                    End If
                    If IsNumeric(Request.Item("khuvuc")) Then
                        khuvucid = Request.Item("khuvuc")
                    End If
                    If IsNumeric(Request.Item("linhvuckd")) Then
                        linhvuckdid = Request.Item("linhvuckd")
                    End If
                    If IsNumeric(Request.Item("quymo")) Then
                        quymoid = Request.Item("quymo")
                    End If
                    KeySearch = Request.Item("timkiem")
                    Me.txtSearch.Text = Request.Item("fullname")

                    'Me.dlKhuVuc.SelectedValue = CInt(Request.Item("khuvuc"))

                    Title = Request.Item("fullname")
                    If IsNumeric(Request.Item("pn")) Then
                        PageIndex = Request.Item("pn")
                    End If
                    If IsNumeric(Request.Item("type")) Then
                        Me.Type = Request.Item("type")
                        If Type = 2 Then
                            Me.plSearchAdv.Visible = True
                        Else
                            Me.plSearchAdv.Visible = False
                        End If

                    End If

                    'Bind category
                    BindKhuVuc()
                    BindQuyMo()
                    BindLinhVucKd()
                    Me.dlKhuVuc.SelectedValue = khuvucid
                    Me.ddlLinhVucKD.SelectedValue = linhvuckdid
                    Me.ddlQuyMo.SelectedValue = quymoid
                    'LoadData Search
                    LoadData()
                End If


            Catch ex As Exception
                Response.Write(ex.ToString)
            End Try

        End Sub
#End Region

        Public Sub LoadData()
            Dim ctlNews As New HoiVienController
            Dim arr As ArrayList
            Dim sFullName As String
            If Me.txtSearch.Text = "" Then
                sFullName = ""
            Else
                sFullName = Me.txtSearch.Text
            End If
            Try
                If Me.plSearchAdv.Visible = True Then
                    
                    arr = ctlNews.FindContent(Me.rbtChoice.SelectedValue, sFullName, Me.dlKhuVuc.SelectedValue, Me.ddlLinhVucKD.SelectedValue, Me.ddlQuyMo.SelectedValue, 0)
                    'arr = ctlNews.FindContent(Me.rbtChoice.SelectedValue, "", 0, 0, 0)
                    Me.drgNews.DataSource = arr
                    Me.drgNews.PageSize = Me.ddlPageSize.SelectedValue
                    Me.drgNews.CurrentPageIndex = PageIndex - 1
                    Me.drgNews.DataBind()

                    Me.ctlPaging.RowCount = arr.Count
                    Me.ctlPaging.PageIndex = PageIndex
                    Me.ctlPaging.PageSize = Me.ddlPageSize.SelectedValue
                    Me.ctlPaging.BindDataPaging()
                Else
                    'Simple Search
                    'Response.Write("Simple Search")
                    arr = ctlNews.FindContent(Me.rbtChoice.SelectedValue, sFullName, Me.dlKhuVuc.SelectedValue, Me.ddlLinhVucKD.SelectedValue, Me.ddlQuyMo.SelectedValue, 0)
                    'arr = ctlNews.FindContent(Me.rbtChoice.SelectedValue, "", 0, 0, 0)
                    Me.drgNews.DataSource = arr
                    Me.drgNews.PageSize = 10
                    Me.drgNews.CurrentPageIndex = PageIndex - 1
                    Me.drgNews.DataBind()

                    Me.ctlPaging.RowCount = arr.Count
                    Me.ctlPaging.PageIndex = PageIndex
                    Me.ctlPaging.PageSize = 10
                    Me.ctlPaging.BindDataPaging()
                End If

            Catch ex As Exception
                Response.Write(ex.ToString)
            End Try

        End Sub

        Public Sub BindKhuVuc()
            'Dim ctlCate As New KhuVucController
            'Dim arr As ArrayList
            'arr = ctlCate.KhuVuc_GetAll()
            'Dim objCate As New KhuVucInfo
            'Me.dlKhuVuc.DataSource = arr
            'Me.dlKhuVuc.DataTextField = "KhuVuc"
            'Me.dlKhuVuc.DataValueField = "Id"
            'Me.dlKhuVuc.DataBind()
            'Me.dlKhuVuc.Items.Insert(0, New ListItem("------Tất cả------", ""))
            Dim ctlCate As New KhuVucController
            Dim arr As ArrayList
            arr = ctlCate.KhuVuc_GetAll(PortalId)
            Dim objCate As New KhuVucInfo
            objCate.id = 0
            objCate.KhuVuc = "    ------Tất cả------"
            arr.Insert(0, objCate)
            Me.dlKhuVuc.DataSource = arr
            Me.dlKhuVuc.DataTextField = "khuvuc"
            Me.dlKhuVuc.DataValueField = "id"
            Me.dlKhuVuc.DataBind()
        End Sub
        Private Sub BindQuyMo()
            Dim ctlStatus As New QuyMoController
            Me.ddlQuyMo.DataSource = ctlStatus.QuyMo_GetAll(PortalId)
            Me.ddlQuyMo.DataTextField = "QuyMo"
            Me.ddlQuyMo.DataValueField = "id"
            Me.ddlQuyMo.DataBind()
            Me.ddlQuyMo.Items.Insert(0, New ListItem("------Tất cả------", ""))
        End Sub
        Private Sub BindLinhVucKd()
            Dim ctlStatus As New LinhVucKDController
            Me.ddlLinhVucKD.DataSource = ctlStatus.LinhVucKD_GetAll(PortalId)
            Me.ddlLinhVucKD.DataTextField = "LinhVucKinhDoanh"
            Me.ddlLinhVucKD.DataValueField = "id"
            Me.ddlLinhVucKD.DataBind()
            Me.ddlLinhVucKD.Items.Insert(0, New ListItem("------Tất cả------", ""))
        End Sub
        Public Function ReplaceChuoi(ByVal chuoi As String) As String
            Dim convert As String = Global.ReplaceChuoi.bodau(chuoi).Replace(" ", "-")
            Return convert
        End Function
        Public Function FormatURL(ByVal sitem As String, ByVal strID As String, ByVal strLink As String) As String
            Return "/" & sitem & "/" & strID & "/" & ReplaceChuoi(strLink) & ".htm"
        End Function
        Public Function GetChucVu(ByVal id As Integer) As String
            Dim ctlChucVu As New BCH_ChucVuController
            Dim objChucVu As BCH_ChucVuInfo
            If id = -1 Then
                Return "Hội viên"
            Else
                objChucVu = ctlChucVu.BCH_ChucVu_GetByID(id)
                Return objChucVu.BCH_ChucVu
            End If

        End Function
        Protected Sub lbtMore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtMore.Click
            If plSearchAdv.Visible = False Then
                plSearchAdv.Visible = True
            Else
                plSearchAdv.Visible = False
            End If
        End Sub

        Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
            Dim sFullName As String
            If Me.txtSearch.Text = "" Then
                sFullName = ""
            Else
                sFullName = Me.txtSearch.Text
            End If
            If plSearchAdv.Visible = True Then
                Response.Redirect(NavigateURL(TabId) & "&timkiem=" & rbtChoice.SelectedValue & "&fullname=" & sFullName & "&khuvuc=" & dlKhuVuc.SelectedValue & "&linhvuckd=" & ddlLinhVucKD.SelectedValue & "&quymo=" & ddlQuyMo.SelectedValue & "&type=2")
            Else
                'Response.Redirect(NavigateURL(TabId) & "/sch/" & rbtChoice.SelectedValue & "/key/" & txtSearch.Text & "/khuvuc/" & dlKhuVuc.SelectedValue & "/linhvuckd/" & ddlLinhVucKD.SelectedValue & "/quymo/" & ddlQuyMo.SelectedValue & "/index.htm")
                Response.Redirect(NavigateURL(TabId) & "&timkiem=" & rbtChoice.SelectedValue & "&fullname=" & sFullName & "&khuvuc=" & dlKhuVuc.SelectedValue & "&linhvuckd=" & ddlLinhVucKD.SelectedValue & "&quymo=" & ddlQuyMo.SelectedValue & "&type=1")
            End If
        End Sub
    End Class

End Namespace
